package cn.signalpha.modules.base.controller.admin;

import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.base.dto.HelpDTO;
import cn.signalpha.modules.base.entity.Help;
import cn.signalpha.modules.base.service.HelpService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/help")
@Api(value = "帮助中心", tags = "帮助中心")
public class HelpController extends BaseController {

	private final HelpService helpService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    @ApiImplicitParams({
		@ApiImplicitParam(name="code", dataType = "string", value = "编码"),
		@ApiImplicitParam(name="title", dataType = "string", value = "标题"),
    })
    public R<IPage<Help>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<Help> queryWrapper = Condition.getQueryWrapper(params, Help.class);
        return R.data(helpService.page(Condition.getPage(query), queryWrapper.lambda().orderByDesc(Help::getSort)));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Help> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(helpService.getById(id));
	}

	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入help")
	public R submit(@Valid @RequestBody HelpDTO help) {
		helpService.removeCaChe();
		return R.status(helpService.saveOrUpdate(BeanUtil.copy(help, Help.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		helpService.removeCaChe();
		return R.status(helpService.deleteLogic(Func.toLongList(ids)));
	}

}
